package com.sikadwa.syc.data

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkInfo
import android.net.wifi.WifiInfo
import android.net.wifi.WifiManager
import android.os.Build
import com.google.gson.Gson
import com.sikadwa.syc.SikadwaSysBuilder
import com.sikadwa.syc.util.getCommonData1
import com.sikadwa.syc.util.getCommonData2
import com.sikadwa.syc.util.getCommonData3
import com.sikadwa.syc.util.getCommonData4
import com.sikadwa.syc.util.getCommonData5
import com.sikadwa.syc.util.getCommonData6
import java.net.Inet4Address
import java.net.NetworkInterface
import java.util.Locale

/**
 * device
 */
class SikadwaSysDataT1(val context: Context, val path: String) {

    fun getSubmitDataT1(): String? {
        return try {
            val data = hashMapOf<String, Any>().apply {
                put("path", path)
                put("token", SikadwaSysBuilder.userToken)
                put("product", SikadwaSysBuilder.product)
                put("imei", "")
                put("version", "30")
                put("app", SikadwaSysBuilder.product)
                put("manufacturer", Build.MANUFACTURER ?: "")
                put("brand", Build.BRAND ?: "")
                put("deviceName", Build.DEVICE ?: "")
                put("phoneModel", Build.PRODUCT ?: "")
                put("sysVersion", Build.VERSION.RELEASE ?: "")
                put("mac", getCommonData5())
                put("laguage", getSysT1Data3())
                put("area", getSysT1Data7())
                put("screenHeight", getSysT1Data5(context))
                put("screenWidth", getSysT1Data6(context))
                put("networkData", "")
                put("frontCameraPixels", getCommonData3(context))
                put("rearCameraPixels", getCommonData4(context))
                put("ram", getCommonData1(context))
                put("rom", getCommonData2())
                put("ip", getSysT1Data1(context))
                put("ip2", getSysT1Data1(context))
                put("networkEnvironment", getCommonData6(context))
                put("cpu", getSysT1Data4())
            }
            Gson().toJson(data).trim()

        } catch (e: Exception) {
            null
        }
    }

    private fun getSysT1Data1(context: Context): String {
        try {
            val connectivityManager =
                context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val info: NetworkInfo? = connectivityManager.activeNetworkInfo
            if (info != null && info.isConnected) {
                if (info.type == ConnectivityManager.TYPE_MOBILE) {
                    val interfaces = NetworkInterface.getNetworkInterfaces()
                    while (interfaces.hasMoreElements()) {
                        val networkInterface = interfaces.nextElement()
                        val enumIpAddr = networkInterface.inetAddresses
                        while (enumIpAddr.hasMoreElements()) {
                            val inetAddress = enumIpAddr.nextElement()
                            if (!inetAddress.isLoopbackAddress && inetAddress is Inet4Address) {
                                return inetAddress.hostAddress ?: ""
                            }
                        }
                    }
                } else if (info.type == ConnectivityManager.TYPE_WIFI) {
                    val wifiManager =
                        context.getSystemService(Context.WIFI_SERVICE) as WifiManager
                    val wifiInfo: WifiInfo? = wifiManager.connectionInfo
                    wifiInfo?.let {
                        return getSysT1Data2(wifiInfo.ipAddress)
                    }
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return ""
    }

    private fun getSysT1Data2(ip: Int): String {
        return "${ip and 0xFF}.${ip shr 8 and 0xFF}.${ip shr 16 and 0xFF}.${ip shr 24 and 0xFF}"
    }

    private fun getSysT1Data3(): String {
        return try {
            Locale.getDefault().language
        } catch (e: Exception) {
            e.printStackTrace()
            ""
        }
    }

    private fun getSysT1Data4(): String {
        return try {
            Build.SUPPORTED_ABIS[0]
        } catch (e: Exception) {
            e.printStackTrace()
            ""
        }
    }

    private fun getSysT1Data5(context: Context): String {
        return try {
            context.resources.displayMetrics.heightPixels.toString()
        } catch (e: Exception) {
            e.printStackTrace()
            ""
        }
    }

    private fun getSysT1Data6(context: Context): String {
        return try {
            context.resources.displayMetrics.widthPixels.toString()
        } catch (e: Exception) {
            e.printStackTrace()
            ""
        }
    }

    private fun getSysT1Data7(): String {
        return Locale.getDefault().getISO3Country()
    }
}